import {getAllRoleList, isAccountExit} from '/@/api/demo/system'
import {BasicColumn} from '/@/components/Table'
import {FormSchema} from '/@/components/Table'
import {Rule} from "/@/components/Form";



export const columns: BasicColumn[] = [
  {
    title: '用户名',
    dataIndex: 'account',
    width: 120,
  },
  {
    title: '姓名',
    dataIndex: 'username',
    width: 120,
  },
  {
    title: '电话',
    dataIndex: 'phone',
    width: 120,
  },
  {
    title: '身份证',
    dataIndex: 'idCard',
    width: 120,
  },
  {
    title: '创建时间',
    dataIndex: 'gmtCreate',
    width: 180,
  },
  {
    title: '角色',
    dataIndex: 'roleCode',
    width: 200,
  },
]
export const passwordFormSchema:FormSchema[]=[
  {
    label: '新密码',
    field:'password',
    required:true,
    component:'InputPassword',
    rules:[
      {
        max:16,
        min:6
      }
    ],
  },
  {
    label:'id',
    field:'id',
    required:true,
    component:'Input',
    show:false
  }
]

export const searchFormSchema: FormSchema[] = [
  {
    field: 'account',
    label: '用户名',
    component: 'Input',
    colProps: {span: 8},
  },
  {
    field: 'username',
    label: '姓名',
    component: 'Input',
    colProps: {span: 8},
  },
]

export const accountFormSchema: FormSchema[] = [
  {
    label:'id',
    field:'id',
    component:'Input',
    show:false,
    //ifShow 不能使用ifShow
  },
  {
    label: '账户',
    field: 'account',
    component: 'Input',
    dynamicRules({model}) {
       const rules:Rule[] =  [{
        required: true,
        message: '请输入账户'
      },
        {
          max: 16,
          message: '最大长度不超过16位'
        },
        {
          min: 6,
          message: '最小长度不超过6位'
        },
        {
          validator: ()=>{
            return isAccountExit({id:model?.id,account:model?.account})
          },
        }
      ]
      return rules
    }
  },
  {
    label: '密码',
    field: 'password',
    component: 'InputPassword',
    rules: [
      {
        required: true,
        message: '请输入密码'
      },
      {
        min: 6,
        message: '密码长度最低为6位'
      },
      {
        max: 16,
        message: '密码长度不能超过20位'
      },

    ]
  },
  {label: '姓名', field: 'username', component: 'Input', required: true},
  {
    label: '角色',
    field: 'roleId',
    component: 'ApiSelect',
    componentProps: {
      api: getAllRoleList,
      labelField: 'roleName',
      valueField: 'id',
    },
    itemProps: { validateTrigger: 'blur' },
    rules:[
      {
        required:true,

      }
    ]
    // dynamicRules({model}){
    //   const temp = model
    //     const rules:Rule[]=[
    //         {
    //             
    //               return new Promise((resolve,reject)=>{
    //                 if(model?.roleId){
    //                   resolve()
    //                 }else{
    //                   reject('请选择角色')
    //                 }
    //               })
    //             }
            
    //         }
    //     ]
    //     return rules
    // }
  
  },
  {
    label: '电话',
    field: 'phone',
    component: 'Input',
    required: true,
    dynamicRules({model}) {
      const rules:Rule[] =  [{
        required: true,
        message: '请输入手机号'
      },
        {
           validator: ()=>{
             return isAccountExit({id:model?.id,phone:model?.phone})
           },
        }
      ]
      return rules
    }
  },
  {
    label: '身份证号',
    field: 'idCard',
    component: 'Input',
    required: true,
    dynamicRules({model}) {
      const rules:Rule[] =  [{
        required: true,
        message: '请输入账户'
      },
        {
          validator: ()=>{
            return  isAccountExit({id:model?.id,idCard:model?.idCard})
          },
        }
      ]
      return rules
    }
  },
]
